<template>
  <el-dialog 
    class="simple-dialog"
    :title="tempData.id ? '修改' : '添加'"
    :visible.sync="tempVisible"
    width="400px"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="formName" :rules="rules" :model="tempData" label-width="100px"> 
      <el-form-item label="卡号:" prop="cardNum" v-if="curUserRole == -1">
        <el-input v-model="tempData.cardNum"/>
      </el-form-item> 
      <el-form-item label="激活码:" prop="activationNum">
        <el-input v-model="tempData.activationNum"/>
      </el-form-item>  
      <el-form-item label="期限:" prop="term">
        <el-input v-model="tempData.term">
          <template slot="append">年</template>
        </el-input>
      </el-form-item> 
    </el-form>
    <span slot="footer" class="dalog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="saveUpdate('formName')">保存</el-button>
    </span>
  </el-dialog>
</template>

<script> 
  import testsApi from '@/api/system/card'
  import publisherApi from '@/api/system/publisher'
  export default {
    name: 'leaveAddEdit',
    props: {
      dialogVisible: {
        type: Boolean,
        default: false
      },
      data: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        tempVisible: false,
        projectLeave:0,
        publisherList:[],
        tempData: {
          id: null
        },
        rules: {
          activationNum: [{ required: true, message: '请输入激活码', trigger: 'blur' }],
          cardNum: [{ required: true, message: '请输入卡号', trigger: 'blur' }],
          term: [{ required: true, message: '请输入期限', trigger: 'blur' }],
        },
        curUserRole: -1
      }
    },
    watch: {
      dialogVisible: function(val) {
        this.tempVisible = val;
      },
      data: function(val) {
        this.tempData = JSON.parse(JSON.stringify(val));
      }
    },
    computed: {
      user() {
        return this.$store.state.user
      }
    },
    mounted() {
      this.curUserRole = this.user.roles.findIndex(item => item == 3)
      // this.getpublisher();
    },
    methods: {
      //获取门店
      getpublisher(){
        var params = {
          page: 1,
          pageSize: 1000,
        }
        console.log(publisherApi,'publisherApipublisherApipublisherApi')
        publisherApi.getPublisher(params).then(res => {
          this.publisherList = res.data
        })
      },
      close() {
        this.$refs['formName'].clearValidate()
        this.$emit('close')
      },
      saveUpdate(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.tempData.id) {
              testsApi.update(this.tempData).then(res => {
                this.$message.success('更新成功')
                this.close()
                this.$emit('reloadTable',res.data)
              }).catch(() => {})
            } else {
              if(this.curUserRole > -1){
                this.tempData.publisherId = this.user.publisherId;
              }
              testsApi.save(this.tempData).then(res => {
                this.$message.success('添加成功')
                this.$emit('close');
                this.$emit('reloadTable',res.data)
              }).catch(() => {
              })
            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style lang="scss">

</style>
